package com.linwen.javase.desgin_pattern.action.iterator;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MyArrayList
 * @Description
 * @Author lin
 * @Date 2022/6/29 上午12:03
 * @Version V1.0
 */
public class MyArrayList implements MyList{
    private List<Object> list = new ArrayList<Object>();

    @Override
    public void add(Object obj) {
        list.add(obj);
    }

    @Override
    public void remove(Object obj) {
        list.remove(obj);
    }

    @Override
    public MyIterator iterator() {
        return new MyArrayIterator(list) ;
    }

    public class MyArrayIterator implements MyIterator{
        private List<Object> list = null;
        private int index = -1;

        public MyArrayIterator(List<Object> list) {
            this.list = list;
        }

        @Override
        public Object next() {
            Object obj = null;
            if (this.hasNext()) {
                obj = list.get(++index);
            }
            return obj;
        }

        @Override
        public boolean hasNext() {
            if (index < list.size() - 1) {
                return true;
            } else {
                return false;
            }
        }
    }
}
